package com.zsrd.lessons.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsrd.core.response.ResponseSupport;
import com.zsrd.lessons.domain.TsExamQuestion;
import com.zsrd.lessons.domain.dto.ExamDto;
import com.zsrd.lessons.domain.dto.ExamPlanDto;
import com.zsrd.lessons.domain.vo.*;
import com.zsrd.lessons.service.TsExamPlanService;
import com.zsrd.lessons.service.TsExamPlanUserService;
import com.zsrd.lessons.service.TsExamService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 试卷表 前端控制器
 * </p>
 *
 * @author frk
 * @since 2022-07-12
 */
@RestController
@RequestMapping("/examManage")
public class ExamManageController {

    @Autowired
    TsExamPlanService tsExamPlanService;
    @Autowired
    TsExamPlanUserService tsExamPlanUserService;
    @Autowired
    TsExamService tsExamService;


    /**
     * 制定考试计划
     *
     * @return
     */
    @PostMapping("/addPlan")
    public ResponseSupport addPlan(@RequestBody ExamPlanDto examPlanDto) {
        try {
            tsExamPlanService.savePlan(examPlanDto);
            return ResponseSupport.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.success(e.getMessage());
        }

    }

    /**
     * 查询考试计划
     *
     * @param examPlanDto
     * @return
     */
    @PostMapping("/queryExamPlan")
    public ResponseSupport<IPage<ExamPlanVo>> queryExamPlan(@RequestBody ExamPlanDto examPlanDto) {
        return ResponseSupport.success("成功",tsExamPlanService.queryExamPlan(examPlanDto));
    }


    /**
     * 删除考试计划
     *
     * @param examPlanDto
     * @return
     */
    @PostMapping("/deleteExamPlan")
    public ResponseSupport deleteExamPlan(@RequestBody ExamPlanDto examPlanDto) {

        return ResponseSupport.success("成功",tsExamPlanService.delPlan(examPlanDto));
    }


    /**
     * 在线考试
     * 生成试卷题目，答案。由前端判断题目对错，计算得分
     *
     * @param examPlanDto
     * @return
     */
    @PostMapping("/createExam")
    public ResponseSupport createExam(@RequestBody ExamPlanDto examPlanDto) {
        ExamQuestionsVo examQuestionsVo = tsExamPlanService.createExam(examPlanDto);
        if (examQuestionsVo == null) {
            return ResponseSupport.fail("失败");
        }
        return ResponseSupport.success("成功",examQuestionsVo);
    }

    /**
     * 考试管理
     *
     * @param examPlanDto
     * @return
     */
    @PostMapping("/queryExam")
    public ResponseSupport<IPage<ExamVo>> queryExam(@RequestBody ExamPlanDto examPlanDto) {
        return ResponseSupport.success("成功",tsExamPlanService.queryExam(examPlanDto));
    }


    /**
     * 我的考试查询
     *
     * @param examPlanDto
     * @return
     */
    @PostMapping("/queryMyExam")
    public ResponseSupport<IPage<ExamVo>> queryMyExam(@RequestBody ExamPlanDto examPlanDto) {
        return ResponseSupport.success("成功",tsExamPlanService.queryMyExam(examPlanDto));
    }

    /**
     * 考试成绩保存
     * 提交成绩，答题明细
     *
     * @param examDto
     * @return
     */
    @PostMapping("/saveResult")
    public ResponseSupport saveResult(@RequestBody ExamDto examDto) {
        return ResponseSupport.success(tsExamPlanService.saveResult(examDto));
    }

    /**
     * 考试成绩保存
     * 提交成绩，答题明细
     *
     * @param
     * @return
     */
    @PostMapping("/saveResultStep")
    public ResponseSupport saveResultStep(@RequestBody TsExamQuestion tsExamQuestion) {
        Boolean b = tsExamPlanService.saveResultStep(tsExamQuestion);
        return ResponseSupport.success("成功");
    }

    /**
     * 获取练习题
     *
     * @return
     */
    @GetMapping("/practiceQuestion")
    public ResponseSupport<TsExamQuestionVo> practiceQuestion() {
        return ResponseSupport.success(tsExamPlanService.practiceQuestion());
    }

    /**
     * 查看考试结果
     *
     * @param id 试卷ID
     * @return
     */
    @GetMapping("/queryExamResult")
    public ResponseSupport<ExamVo> queryExamResult(@Param(value = "id") int id) {
        return ResponseSupport.success(tsExamPlanService.queryExamResult(id));
    }

    /**
     * 获取考试人员
     *
     * @param orgNo
     * @return
     */
    @RequestMapping("/getExamUser")
    public ResponseSupport getExamUser(String orgNo) {
        try {
            List<ExamPlanUserVo> list = tsExamPlanUserService.getExamUser(orgNo);
            return ResponseSupport.success(list);
        } catch (Exception e) {
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 查询即将开始的考试 包含30分钟之外
     *
     * @param orgNo
     * @return
     */
    @RequestMapping("/queryBeStartExam")
    public ResponseSupport queryBeStartExam(String orgNo, String loginId) {
        try {
            ExamVo t = tsExamService.queryBeStartExam(orgNo, loginId);
            return ResponseSupport.success(t);
        } catch (Exception e) {
            return ResponseSupport.fail(e.getMessage());
        }
    }
}
